<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文本编辑工具</title>
    <link rel="stylesheet" href="../css/all.min.css">
    <style>
        body {
            font-family: 'Arial', sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            background-color: #fff;
            display: flex;
            justify-content: center;
            height: 100vh;
            box-sizing: border-box;
        }
        .main-container {
            display: flex;
            width: 100%;
            gap: 20px;
            height: calc(100% - 40px);
        }
        .container {
            flex: 1;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            height: 100%;
        }
        .preview-container {
            width: 50%;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            height: 100%;
        }
        .preview-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
            text-align: center;
        }
        .phone-preview {
            background-color: #f0f0f0;
            border: 10px solid #764ba2;
            border-radius: 30px;
            width: 380px;
            height: calc(100% - 45px);
            margin: 0 auto;
            padding: 15px;
            overflow-y: auto;
            position: relative;
            flex-grow: 1;
        }
        .phone-content {
            font-family: 'Microsoft YaHei', sans-serif;
            font-size: 16px;
            line-height: 1.8;
            color: #333;
            white-space: pre-wrap;
            min-height: 100%;
        }
        .toolbar {
            display: flex;
            flex-wrap: wrap;
            gap: 5px;
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }
        button {
            padding: 8px 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            color: #fff;
            transition: all 0.2s;
            border:0;
        }
        button:hover {
            background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
        }
        select {
            padding: 8px;
            border-radius: 4px;
            border: 1px solid #ddd;
        }
        #editor {
            min-height: 200px;
            height: calc(100% - 150px); /* 减去工具栏和底部区域高度 */
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            outline: none;
            overflow-y: auto;
            flex-grow: 1;
        }
        .word-count {
            margin-top: 10px;
            text-align: right;
            color: #666;
        }
        .file-actions {
            flex-wrap: wrap;
            display: flex;
            gap: 10px;
            position: fixed;
            right: 20px;
            top: 50%;
            flex-direction: column;
        }
        
        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1000;
            justify-content: center;
            align-items: center;
        }
        .modal-content {
            background-color: white;
            padding: 25px;
            border-radius: 8px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
            width: 400px;
            max-width: 90%;
            animation: modalFadeIn 0.3s;
        }
        @keyframes modalFadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .modal-header {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 15px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .modal-close {
            font-size: 24px;
            cursor: pointer;
            color: #999;
        }
        .modal-close:hover {
            color: #333;
        }
        .modal-body {
            margin-bottom: 20px;
        }
        .modal-footer {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }
        .modal-input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="main-container">
        <div class="container">
            <div class="toolbar">
                <button id="bold" title="加粗"><i class="fas fa-bold"></i></button>
                <button id="italic" title="斜体"><i class="fas fa-italic"></i></button>
                <button id="underline" title="下划线"><i class="fas fa-underline"></i></button>
                <select id="heading">
                    <option value="">正文</option>
                    <option value="h1">标题1</option>
                    <option value="h2">标题2</option>
                    <option value="h3">标题3</option>
                </select>
                <select id="fontSize">
                    <option value="">字体大小</option>
                    <option value="1">小</option>
                    <option value="3">中</option>
                    <option value="5">大</option>
                    <option value="7">特大</option>
                </select>
                <select id="fontFamily">
                    <option value="">字体</option>
                    <option value="Arial">Arial</option>
                    <option value="Times New Roman">Times New Roman</option>
                    <option value="Courier New">Courier New</option>
                    <option value="宋体">宋体</option>
                    <option value="微软雅黑">微软雅黑</option>
                </select>
                <button id="undo" title="撤销"><i class="fas fa-undo"></i></button>
                <button id="redo" title="重做"><i class="fas fa-redo"></i></button>
            </div>
            
            <div id="editor" contenteditable="true"></div>
            
            <div class="word-count">
                字数: <span id="count">0</span>
            </div>
            
            <div class="file-actions">
                <button id="load"><i class="fas fa-folder-open"></i> 打开</button>
                <button id="exportTxt"><i class="fas fa-file-export"></i> 导出</button>
            </div>
        </div>
        
        <div class="preview-container">
            <div class="preview-title">预览</div>
            <div class="phone-preview">
                <div class="phone-content" id="phonePreview"></div>
            </div>
        </div>
    </div>
    
    <!-- 文件名输入模态框 -->
    <div class="modal" id="filenameModal">
        <div class="modal-content">
            <div class="modal-header">
                <span>输入文件名</span>
                <span class="modal-close">&times;</span>
            </div>
            <div class="modal-body">
                <input type="text" id="filenameInput" class="modal-input" placeholder="请输入文件名（不含扩展名）" value="">
            </div>
            <div class="modal-footer">
                <button class="btn-secondary" id="cancelFilename">取消</button>
                <button class="btn-primary" id="confirmFilename">确定</button>
            </div>
        </div>
    </div>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const editor = document.getElementById('editor');
            const wordCount = document.getElementById('count');
            const phonePreview = document.getElementById('phonePreview');
            
            // 工具栏功能
            document.getElementById('bold').addEventListener('click', function() {
                document.execCommand('bold', false, null);
            });
            
            document.getElementById('italic').addEventListener('click', function() {
                document.execCommand('italic', false, null);
            });
            
            document.getElementById('underline').addEventListener('click', function() {
                document.execCommand('underline', false, null);
            });
            
            document.getElementById('heading').addEventListener('change', function() {
                const format = this.value;
                document.execCommand('formatBlock', false, format || 'p');
            });
            
            document.getElementById('fontSize').addEventListener('change', function() {
                const size = this.value;
                if (size) document.execCommand('fontSize', false, size);
            });
            
            document.getElementById('fontFamily').addEventListener('change', function() {
                const font = this.value;
                if (font) document.execCommand('fontName', false, font);
            });
            
            document.getElementById('undo').addEventListener('click', function() {
                document.execCommand('undo', false, null);
            });
            
            document.getElementById('redo').addEventListener('click', function() {
                document.execCommand('redo', false, null);
            });
            
            // 字数统计（按中文字符计算）和实时预览
            editor.addEventListener('input', function() {
                updateWordCount();
                updatePhonePreview();
            });
            updateWordCount();
            updatePhonePreview();
            
            function updateWordCount() {
                const text = editor.innerText;
                // 中文按字符计算，英文按单词计算
                const count = text.replace(/[^\u4e00-\u9fa5]/g, '').length + 
                             text.replace(/[\u4e00-\u9fa5]/g, '').trim().split(/\s+/).filter(word => word.length > 0).length;
                wordCount.textContent = count;
            }
            
            function updatePhonePreview() {
                // 简单处理HTML内容以适应手机预览
                let content = editor.innerHTML;
                // 替换标题样式为简单的加粗
                content = content.replace(/<h1[^>]*>/g, '<div style="font-weight:bold;font-size:1.3em;margin:15px 0 10px 0;">')
                                .replace(/<h2[^>]*>/g, '<div style="font-weight:bold;font-size:1.2em;margin:12px 0 8px 0;">')
                                .replace(/<h3[^>]*>/g, '<div style="font-weight:bold;font-size:1.1em;margin:10px 0 6px 0;">')
                                .replace(/<\/h[1-3]>/g, '</div>')
                                .replace(/<p[^>]*>/g, '<div style="margin-bottom:8px;">')
                                .replace(/<\/p>/g, '</div>');
                phonePreview.innerHTML = content || '<div style="color:#999;text-align:center;margin-top:50%;">输入内容将在此预览</div>';
            }
            
            // 文件操作
            document.getElementById('load').addEventListener('click', function() {
                const input = document.createElement('input');
                input.type = 'file';
                input.accept = '.txt,text/plain';
                
                input.onchange = e => {
                    const file = e.target.files[0];
                    const reader = new FileReader();
                    
                    reader.onload = function(event) {
                        editor.innerHTML = event.target.result.replace(/\n/g, '<br>');
                        updateWordCount();
                        updatePhonePreview();
                    };
                    
                    reader.readAsText(file);
                };
                
                input.click();
            });
            
            // 导出TXT功能（带自定义文件名）
            document.getElementById('exportTxt').addEventListener('click', function() {
                showFilenameModal();
            });
            
            // 模态框操作
            const filenameModal = document.getElementById('filenameModal');
            const filenameInput = document.getElementById('filenameInput');
            
            function showFilenameModal() {
                filenameModal.style.display = 'flex';
                filenameInput.focus();
            }
            
            function hideFilenameModal() {
                filenameModal.style.display = 'none';
            }
            
            // 关闭按钮事件
            document.querySelectorAll('.modal-close').forEach(closeBtn => {
                closeBtn.addEventListener('click', function() {
                    const modal = this.closest('.modal');
                    modal.style.display = 'none';
                });
            });
            
            // 文件名模态框按钮事件
            document.getElementById('confirmFilename').addEventListener('click', function() {
                const filename = filenameInput.value.trim() || 'document';
                exportAsTxt(filename);
                hideFilenameModal();
            });
            
            document.getElementById('cancelFilename').addEventListener('click', hideFilenameModal);
            
            // 点击模态框外部关闭
            window.addEventListener('click', function(event) {
                if (event.target === filenameModal) {
                    hideFilenameModal();
                }
            });
            
            // 导出TXT文件
            function exportAsTxt(filename) {
                const text = editor.innerText;
                const blob = new Blob([text], { type: 'text/plain' });
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = `${filename}.txt`;
                a.click();
                URL.revokeObjectURL(url);
            }
        });
    </script>
</body>
</html>